package com.hackerrank.contests.july13.challenges.hackerranknumber;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

public class BruteSolution {
	private static int numDigs(int N) {
		int c = 0;
		while(N > 0) {
			c++;
			N /= 10;
		}
		return c;
	}
	public static BigInteger answer(int A, int B) {
		Set<Long> la = new HashSet<Long>(), lb = new HashSet<Long>();
		int c[] = new int[A];
		BigInteger s[][] = new BigInteger[A][A];
		for(int i = 1; i <= A; i++) {
			s[i - 1] = new BigInteger[A];
			for(long j = 1; j <= B; j++) {
				if(la.add(i * j)) {
					int ind = (int)((i * j - 1) / B);
					c[ind]++;
					if(s[i - 1][ind] == null)
						s[i - 1][ind] = BigInteger.ZERO;
					s[i - 1][ind] = s[i - 1][ind].add(new BigInteger(Long.toString(i * j)));
				}
				lb.add(i ^ j);
			}
		}
		BigInteger sa = BigInteger.ZERO, sb = BigInteger.ZERO;
		for(long n : la)
			sa = sa.add(new BigInteger(Long.toString(n)));
		for(long n : lb)
			sb = sb.add(new BigInteger(Long.toString(n)));
//		Long[] aa = new Long[la.size()], ab = new Long[lb.size()];
//		aa = la.toArray(aa);
//		ab = lb.toArray(ab);
//		Arrays.sort(aa);
//		Arrays.sort(ab);
//		System.out.println("la: " + Arrays.toString(aa));
//		System.out.println("lb: " + Arrays.toString(ab));
//		System.out.println("c: " + Arrays.toString(c));
//		System.out.println("s: " + Arrays.toString(s));
//		System.out.println("sa: " + sa + ", sb: " + sb);
//		System.out.println("ca: " + la.size() + ", cb: " + lb.size());
		return sa.multiply(new BigInteger(Integer.toString(lb.size()))).multiply(new BigInteger(Integer.toString((int)Math.pow(10, numDigs(B))))).add(sb.multiply(new BigInteger(Integer.toString(la.size()))));
	}
	public static void main(String[] args) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter("D:/hackerranknumbersolutions.txt"));
		for(int B = 1; B < 1000000; B++)
			for(int A = 1; A <= 30; A++) {
				bw.write(A + " " + B + "\n");
				BigInteger a1 = answer(A, B), a2 = Solution.answer(A, B);
				if(a1.compareTo(a2) == 0)
					bw.write("Passed: " + a1 + "\n");
				else
					bw.write("Failed: " + a1 + ", " + a2 + "\n");
			}
		bw.close();
	}
}
